<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Post\SendVip;
use App\User;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\DB;

class UserController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '用户概要';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new User());

        $grid->model()->orderBy('id', 'desc');

        $grid->header(function ($query) {
            $allCount = DB::table('users')->count();
            $notOrderCount = DB::table('users')->whereNull('last_order_time')->count();
            return "全部用户：{$allCount}、未下单用户：{$notOrderCount}";
        });


        $grid->filter(function($filter) {

            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            // 设置datetime类型
            $filter->between('created_at', '成为会员时间')->datetime();
            $filter->between('last_order_time', '上次下单时间')->datetime();
            $filter->between('vip_overdue', '会员到期时间')->datetime();
            //
            $filter->where(function($query){
                $query->where('id', $this->input)->orWhere('name', $this->input)->orWhere('phone', $this->input);
            }, '关键词ID/昵称/手机号');


        });
        $grid->model()->collection(function(Collection $collection){
            $data = DB::table('users_orders')->groupBy('uid')->pluck(DB::raw('count(*) as total'), 'uid');
            foreach ($collection as $item) {
                $item->sellNum = (isset($data[$item->id]) ? $data[$item->id] : 0);
            }
            return $collection;
        });

        $grid->column('name', '用户')->display(function($value){
            return "{$value}<br/>{$this->phone}";
        });
        $grid->column('gender', '性别')->display(function($gender){
            $arr = ['保密', '男' , '女'];
            return $arr[$gender];
        });
        $grid->column('balance', '乐淘币');
        $grid->column('sellNum', '下单次数');
        $grid->column('last_order_time', '上次下单时间');
        $grid->column('vip_overdue', '会员到期时间');
        $states = [
            'on'  => ['value' => 1, 'text' => '加入黑名单', 'color' => 'primary'],
            'off' => ['value' => 5, 'text' => '取消拉黑', 'color' => 'default'],
        ];
        $grid->column('status', '是否黑名单')->switch($states);
        $grid->actions(function ($actions) {
            // 去掉删除
            $actions->disableDelete();
            // 去掉编辑
            $actions->disableEdit();

            $actions->add(new SendVip());
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(User::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('name', '名称');
        $show->field('created_at', '成为用户时间');
        $show->field('last_login', '最近浏览时间');
        $show->field('vip_overdue', '会员过期时间');
        $show->friendNum('好友数')->unserialize('friendNum');
        $show->source('来源')->unserialize('source');
        $show->field('phone', '手机号');
        $show->gender()->using(['0'=>'保密','1' => '男', '2' => '女']);
        $show->avatar()->image();
        $show->divider();
        $show->field('balance', '乐淘币余额');
        $show->allCoin('累计获得乐淘点')->unserialize('allCoin');
        $show->inviteCoin('赚的好友乐淘币')->unserialize('inviteCoin');
        $show->exchange('累计兑换金额')->unserialize('exchange');
        $show->orderNum('累计下单数')->unserialize('orderNum');
        $show->field('last_order_time', '最近下单时间');

        $show->panel()
            ->tools(function ($tools) {
                $tools->disableEdit();
                $tools->disableList();
                $tools->disableDelete();
            });;

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new User());

        $form->text('name', __('Name'));
        $form->email('email', __('Email'));
        $form->password('password', __('Password'));
        $form->text('remember_token', __('Remember token'));

        return $form;
    }
}
